package com.smartthinking.CICPNL.home.activity;

import java.util.ArrayList;
import java.util.Dictionary;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.smartthinking.CICPNL.R;
import com.smartthinking.CICPNL.home.objects.App_ImageLoader;
import com.smartthinking.CICPNL.home.xml.saxXML;

public class Directorio extends Activity {
	public static ArrayList<Dictionary> Actividades = null;
	private Dictionary dic;
	public ListView list;
	public Hoteles_Adapter adapter;
	public Dialog progressDialog;
	public App_ImageLoader imageLoader;
	private Intent intent;

	public void voidFinish(View V) {
		finish();

	}	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		// Checks the orientation of the screen
		 
	}

	public void abrirMenuLateral(View V) {
		intent = new Intent(Directorio.this, MenuLateral.class);

		startActivity(intent);
	}

	@Override
	public void onBackPressed() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_directorio);
		
		

		// "select * from expositores where eve_idr = %@ order by exp_nombre"

		CargarContenido();

	}

	private void CargarContenido() {
		// TODO Auto-generated method stub
		new AsyncActividades().execute("contenido");

	}

	private class AsyncActividades extends AsyncTask<String, Void, Integer> {

		protected void onPostExecute(Integer in) {

			list = (ListView) findViewById(R.id.list);
			adapter = new Hoteles_Adapter(Actividades);
			list.setAdapter(adapter);

			list.setTextFilterEnabled(true);

			progressDialog.dismiss();

			// abrirSplashBanner();
		}

		protected void onPreExecute() {

			progressDialog = ProgressDialog.show(Directorio.this, "",
					"Cargando...");
		}

		@SuppressWarnings("rawtypes")
		protected Integer doInBackground(String... urls) {

			String qry_url = getString(R.string.QRY_URL)
					+ Uri.encode("select * from cintermex_telefonos");

			saxXML saxparser = new saxXML(qry_url);

			Actividades = saxparser.parse();

			return null;

		}

	}

	public class Hoteles_Adapter extends BaseAdapter {

		private LayoutInflater inflater = null;
		private ArrayList<Dictionary> data;

		public Hoteles_Adapter(ArrayList<Dictionary> d) {

			data = d;
			inflater = (LayoutInflater) Directorio.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			imageLoader = new App_ImageLoader(
					Directorio.this.getApplicationContext());

		}

		public int getCount() {
			return data.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public class ViewHolder {

			public TextView tvTitulo;
			public TextView tvTelefono;
			public TextView tvMail;
			public ImageView ivLlamar;

		}

		public View getView(int position, View convertView, ViewGroup parent) {

			View vi = convertView;
			ViewHolder holder;

			if (convertView == null) {

				vi = inflater.inflate(R.layout.ui_directorio_items, null);

				holder = new ViewHolder();

				holder.tvTitulo = (TextView) vi
						.findViewById(R.id.tv_directorioitems_titulo);

				holder.tvTelefono = (TextView) vi
						.findViewById(R.id.tv_directoriointems_telefonocontacto);

				holder.tvMail = (TextView) vi
						.findViewById(R.id.tv_directorioitems_mailcontacto);

				holder.ivLlamar = (ImageView) vi
						.findViewById(R.id.iv_directorio_llamar);

				vi.setTag(holder);

			} else

				holder = (ViewHolder) vi.getTag();

			holder.tvTitulo.setText(data.get(position).get("cint_area")
					.toString());

			holder.tvTelefono.setText(data.get(position).get("cint_tel")
					.toString());
			holder.tvMail.setText(data.get(position).get("cint_mail")
					.toString());

			final String tel = holder.tvTelefono.getText().toString();

			holder.ivLlamar.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub

					Intent callIntent = new Intent();

					callIntent = new Intent(Intent.ACTION_DIAL,

					Uri.parse("tel:" + tel));

					startActivity(callIntent);

				}
			});

			return vi;

		}
	}

}
